package com.example.websocketclusterspringbootstarter.utils;

import com.example.websocketclusterspringbootstarter.exception.WsClusterException;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zhucc
 * @date 2022-03-05 00:14
 */
public class EventClassUtil {


    private EventClassUtil() {
    }

    private static final ConcurrentHashMap<String, Class<?>> MAP = new ConcurrentHashMap<>();

    public static void addEventClass(String eventName, Class<?> aClass) {
        MAP.put(eventName, aClass);
    }

    public static ConcurrentHashMap<String, Class<?>> allClass() {
        return MAP;
    }


    /**
     * 根据eventName获取获取 实体的class
     *
     * @param eventName 事件名称
     * @return eventName对对应实体的class
     */
    public static Class<?> getEventClass(String eventName) {
        Class<?> eventClass = MAP.get(eventName);
        if (null == eventClass) {
            throw new WsClusterException(eventName + " eventEntity not exist");
        }
        return eventClass;
    }

}
